//
//  RSMeCell.m
//  RenShineDoctor
//
//  Created by 黄强强 on 15/11/5.
//  Copyright © 2015年 杭州岐黄信息技术有限公司. All rights reserved.
//

#import "HQQSettingCell.h"
#import "HQQSettingItem.h"
#import "HQQSettingArrowItem.h"
#import "HQQSettingBadgeView.h"
#import "HQQSettingLabelItem.h"
#import "HQQSettingSwitchItem.h"
#import "HQQSettingSwitch.h"
#import "HQQSettingConfig.h"
#import <HQQCommonTools.h>
#import "HQQSettingApperance.h"

@interface HQQSettingCell ()
@property (nonatomic, strong) HQQSettingBadgeView *badgeView;
@property (nonatomic, strong) UIImageView *arrowView;
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, strong) HQQSettingSwitch *rightSwitch;
@end

@implementation HQQSettingCell

- (HQQSettingSwitch *)rightSwitch
{
    if (!_rightSwitch) {
        _rightSwitch = [[HQQSettingSwitch alloc] init];
        [_rightSwitch setOn:YES];
        [_rightSwitch addTarget:self action:@selector(switchStatusChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _rightSwitch;
}

- (UILabel *)rightLabel
{
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.font = [HQQSettingApperance sharedInstance].rightLabelFont;
        _rightLabel.textColor = [HQQSettingApperance sharedInstance].rightLabelColor;
        _rightLabel.hqq_width = 100;
        _rightLabel.hqq_height = 44;
        _rightLabel.textAlignment = NSTextAlignmentRight;
    }
    return _rightLabel;
}

- (UIImageView *)arrowView
{
    if (!_arrowView) {
        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:HQQSettingCellRightArrowImage]];
    }
    return _arrowView;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    HQQSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:RSCommonCellID];
    if (cell == nil) {
        cell = [[HQQSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RSCommonCellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        HQQSettingBadgeView *badgeView = [HQQSettingBadgeView badgeView];
        [self addSubview:badgeView];
        self.badgeView = badgeView;
        
        self.textLabel.font = [HQQSettingApperance sharedInstance].titleLabelFont;
        self.textLabel.textColor = [HQQSettingApperance sharedInstance].titleLabelColor;
    }
    return self;
}

- (void)setItem:(HQQSettingItem *)item
{
    _item = item;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.textLabel.text = item.title;
    self.imageView.image = [UIImage imageNamed:item.icon];
    if ([item.badgeValue isEqualToString:@"已认证"]) {
        [self.badgeView setBackgroundImage:[UIImage hqq_resizedImageWithNamed:@"main_badge_defaul"] forState:UIControlStateNormal];
    }else{
        [self.badgeView setBackgroundImage:[UIImage hqq_resizedImageWithNamed:@"main_badge"] forState:UIControlStateNormal];
    }
    self.badgeView.badgeValue = item.badgeValue;
    
    self.accessoryView.hidden = NO;
    
    // 设置右边的类型
    if ([item isKindOfClass:[HQQSettingArrowItem class]]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
//        self.accessoryView = self.arrowView;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }else if ([item isKindOfClass:[HQQSettingLabelItem class]]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
        
        HQQSettingLabelItem *labelItem = (HQQSettingLabelItem *)item;
        self.rightLabel.text = labelItem.rightLabel;
        self.rightLabel.frame = [self.rightLabel.text hqq_rectWithFont:self.rightLabel.font];
        self.accessoryView = self.rightLabel;
        
    }else if ([item isKindOfClass:[HQQSettingSwitchItem class]]) {
        
        HQQSettingSwitchItem *switchItem = (HQQSettingSwitchItem *)item;
        self.rightSwitch.type = switchItem.type;
        [self.rightSwitch setOn:switchItem.on];
        self.accessoryView = self.rightSwitch;
        
    }else {
        
        self.accessoryView.hidden = YES;
        
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 计算label的宽度
    self.textLabel.hqq_width = [self.textLabel.text hqq_rectWithFont:self.textLabel.font].size.width;
    
    // 计算 badgeView 的位置
    self.badgeView.center = CGPointMake(CGRectGetMaxX(self.textLabel.frame) + self.badgeView.hqq_width / 2 + 10, self.hqq_height / 2);
}

- (void)switchStatusChange:(HQQSettingSwitch *)currentSwitch
{
    HQQSettingSwitchItem *switchItem = (HQQSettingSwitchItem *)self.item;
    
    if (switchItem.switchStatusChangeBlock) {
        switchItem.switchStatusChangeBlock(currentSwitch.isOn);
    }else{
        if ([self.delegate respondsToSelector:@selector(commonCellDidChangeSwitch:)]) {
            [self.delegate commonCellDidChangeSwitch:currentSwitch];
        }
    }
}

//- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {}

@end
